package site.luojie.business.email.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import site.luojie.common.dto.SendVerifyCodeDto;

import java.util.Date;

/**
 * @author jie.luo
 * @since 2020/9/16
 */
@Component
public class MessageConsumerHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageConsumerHandler.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("verifyCodeTopic.default"),
            exchange = @Exchange("verifyCodeTopic")
    ))
    public void sendVerifyCode(SendVerifyCodeDto payload) {
        LOGGER.debug("message ：{}", payload);

        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("这是一封测试邮件");
        // 设置邮件发送者
        message.setFrom("190642964@qq.com");
        // 设置邮件接收者，可以有多个接收者
        message.setTo(payload.getEmail());
        // 设置邮件抄送人，可以有多个抄送人
//        message.setCc("37xxxxx37@qq.com");
        // 设置隐秘抄送人，可以有多个
//        message.setBcc("14xxxxx098@qq.com");
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText("这是的验证码：" + payload.getVerifyCode());
        javaMailSender.send(message);
    }
}
